package com.mm.controller.other;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.AopLog;
import com.mm.base.config.minio.MinIoProperties;
import com.mm.domain.common.DCommonCode;
import com.mm.domain.taxi.DCarInfo;
import com.mm.serivce.common.IDCommonCodeService;
import com.mm.util.result.DPage;
import com.mm.util.result.DResult;
import com.mm.serivce.common.TokenService;
import com.mm.util.common.MinIoUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 描述: 公共数值获取
 *
 * @author: yanglin
 * @Date: 2021-08-31-9:34
 * @Version: 1.0
 */
@Api(value = "/mp/common/v1", tags = {"公共数值获取"})
@Slf4j
@RestController
@RequestMapping("/mp/common/v1")
@AopLog(type = "公共数值相关", stackTraceOnErr = true)
public class CommonController {
    private final TokenService tokenService;
    private final MinIoProperties minIoProperties;
    private final MinIoUtil minIoUtil;

    public CommonController(TokenService tokenService, MinIoProperties minIoProperties, MinIoUtil minIoUtil) {
        this.tokenService = tokenService;
        this.minIoProperties = minIoProperties;
        this.minIoUtil = minIoUtil;
    }

    @ApiOperation(value = "获取Token接口", notes = "", httpMethod = "GET")
    @GetMapping("/getToken")
    public DResult getWechatToken() {
        String token = tokenService.createToken();
        return DResult.of(token);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "HttpServletRequest", dataTypeClass = HttpServletRequest.class, name = "request", value = "")
    })
    @ApiOperation(value = "获取Token接口(Params)", notes = "", httpMethod = "GET")
    @GetMapping("/getTokenByParams")
    public DResult getTokenByParams(HttpServletRequest request) {
        String token = tokenService.createToken(request);
        return DResult.of(token);
    }

    /**
     * @param file
     * @return
     * @RequestPart("file") @RequestParam("file")
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", dataType = "file", dataTypeClass = MultipartFile.class, name = "file", value = "")
    })
    @ApiOperation(value = "MinIo上传文件", notes = "", httpMethod = "POST")
    @PostMapping(value = "/upload")
    public DResult upload(@RequestPart("file") MultipartFile file) {
        String fileUrl = minIoUtil.upload(minIoProperties.getBucketName(), file);
        return DResult.of(fileUrl);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "fileName", value = ""),
            @ApiImplicitParam(paramType = "query", dataType = "HttpServletResponse", dataTypeClass = HttpServletResponse.class, name = "response", value = "")
    })
    @ApiOperation(value = "MinIo下载文件", notes = "", httpMethod = "GET")
    @GetMapping(value = "/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        minIoUtil.download(minIoProperties.getBucketName(), fileName, response);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", dataTypeClass = String.class, name = "fileName", value = "")
    })
    @ApiOperation(value = "MinIo删除文件", notes = "", httpMethod = "GET")
    @GetMapping(value = "/delete")
    public DResult delete(@RequestParam("fileName") String fileName) {
        minIoUtil.deleteFile(minIoProperties.getBucketName(), fileName);
        return DResult.success();
    }
}
